from db.db_helper import conn
import sys
import traceback
from tkinter import messagebox
from flask_tables.gate_pass_info_table import process_record
from flask_tables.gate_pass_info_table import record_commit
from flask_tables.gate_pass_info_table import recover
# from flask_tables.gate_pass_info_mem_table import commit_record

recover = False

j = 0
for i in range(100000, 1664200000, 100000):
    # for i in range(1, 2, 100000):
    file = 'D:/PycharmProjects/PyTorchTest/read_test/202205part2.csv_results/202205part2.csv-{}-{}.CSV'.format(i,
                                                                                                               i + 100001)
    # file = 'D:/PycharmProjects/PyTorchTest/read_test/202205part2.csv_results/202205part2.csv-1-100000.CSV'
    with open(file, 'r',
              encoding='utf8') as f:
        line_count = 0
        line_number = 0
        try:
            for line in f:
                line_number += 1
                # if number == 1:
                #     continue

                if line == '"\n':
                    continue

                if len(line) < 10:
                    print("line len < 10, file = {}, number = {}, line={}".format(file, line_number, line))
                    continue

                line = line.replace('"', '').replace('\n', '').replace("'", "")
                line_split = line.split(',')
                process_record(line_split)
                line_count += 1
        except Exception as e:
            messagebox.showinfo("提示", "发生错误")
            print("insert exception, file = {}, number = {}, line={}".format(file, line_number, line))
            traceback.print_exc()
            sys.exit(-1)

    record_commit()
    conn.commit()
    print('{} finis, line count is {}'.format(file, line_count))

conn.close()